##Replication Code for Tables and Calculations for Chapter 2, "Foreign and Domestic Investment Investment in Argentina"(2014), by Alison E. Post##

library(foreign)

##load replication data set##
corrs <- read.csv("replicationdata.csv", header=TRUE)
attach(corrs)


##FIGURE 2.2: CLUSTERING OF CONTRACTUAL OUTCOMES BY CONCESSION##
clusterdata <- read.csv("replicationdata.csv", header=TRUE)
attach(clusterdata)
clusterdata <- na.omit(data.frame(PostCrisis_RenegFinal,PostCrisis_Tariff_Percent, Exit, Exit_Symbol))
plot(jitter(clusterdata$PostCrisis_RenegFinal), clusterdata$PostCrisis_Tariff_Percent, main="Figure 2.2. Clustering of Contractual Outcomes by Investor", xlab="Progress Toward Contract Renegotiation Post-Crisis", ylab="Total Percentage Tariff Increase Post-Crisis", pch=as.numeric(clusterdata$Exit_Symbol))
legend(0.05,175,c("Lead investor remains","Exit via sale or nationalization"), pch=c(1,19)) 

###TABLE 2.3:  Investor Type and Contractual Outcomes###

#Creating percentages for general table#
	#Exit#
local <- corrs[corrs$Investor_Type=="Local",]
local.cancellation.rate <- sum(local$Weight*local$Exit)/(sum(local$Weight))
domestic <- corrs[corrs$Investor_Type=="Domestic",]
domestic.cancellation.rate <- sum(domestic$Weight*domestic$Exit)/(sum(domestic$Weight))
intl <- corrs[corrs$Investor_Type=="International",]
intl.cancellation.rate <- sum(intl$Weight*intl$Exit)/(sum(intl$Weight))
	#Renegotiation#
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, PostCrisis_RenegFinal))
detach(corrs)
local <- Reneg.subset[Reneg.subset$Investor_Type=="Local",]
local.reneg.rate <- sum(local$Weight*local$PostCrisis_RenegFinal)/(sum(local$Weight))
domestic <- Reneg.subset[Reneg.subset$Investor_Type=="Domestic",]
domestic.reneg.rate <- sum(domestic$Weight*domestic$PostCrisis_RenegFinal)/(sum(domestic$Weight))
intl <- Reneg.subset[Reneg.subset$Investor_Type=="International",]
intl.reneg.rate <- sum(intl$Weight*intl$PostCrisis_RenegFinal)/(sum(intl$Weight))
	#Tariff Increases#
attach(corrs)
Tariff.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, PostCrisis_Tariff_Percent))
detach(corrs)
local <- Tariff.subset[Tariff.subset$Investor_Type=="Local",]
local.tariff <- sum(local$Weight*local$PostCrisis_Tariff_Percent)/(sum(local$Weight))
domestic <- Tariff.subset[Tariff.subset$Investor_Type=="Domestic",]
domestic.tariff.rate <- sum(domestic$Weight*domestic$PostCrisis_Tariff_Percent)/(sum(domestic$Weight))
intl <- Tariff.subset[Tariff.subset$Investor_Type=="International",]
intl.tariff.rate <- sum(intl$Weight*intl$PostCrisis_Tariff_Percent)/(sum(intl$Weight))
	#Note that Azurix's inability to secure a tariff increase in 2002 is excluded from the analysis because company management had already announced its intention to leave the country prior to the crisis##

##Re-coding renegotiations as 0/1 variable##
corrs$PostCrisis_Reneg_Binary <- as.numeric(corrs$PostCrisis_RenegFinal==1|corrs$PostCrisis_RenegFinal==0.75)
	#Renegotiation#
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, PostCrisis_Reneg_Binary))
detach(corrs)
local <- Reneg.subset[Reneg.subset$Investor_Type=="Local",]
local.reneg.rate <- sum(local$Weight*local$PostCrisis_Reneg_Binary)/(sum(local$Weight))
domestic <- Reneg.subset[Reneg.subset$Investor_Type=="Domestic",]
domestic.reneg.rate <- sum(domestic$Weight*domestic$PostCrisis_Reneg_Binary)/(sum(domestic$Weight))
intl <- Reneg.subset[Reneg.subset$Investor_Type=="International",]
intl.reneg.rate <- sum(intl$Weight*intl$PostCrisis_Reneg_Binary)/(sum(intl$Weight))
	#Anova for Investor Type and Post-Crisis Contract Renegotiation#
lm.out <- lm(PostCrisis_Reneg_Binary ~ Investor_Type, weights=Weight, data=corrs)
summary(lm.out)
anova.lm(lm.out)


##Breaking down exits for the pre- and post-crisis period##

	#exits pre-crisis#
corrs$Exit_precrisis <- as.numeric(corrs$Exit_Year<=2002) #includes Azurix#
pre_crisiscases <- corrs[corrs$Post_Crisis_Entry==0,]
	
local <- pre_crisiscases[pre_crisiscases$Investor_Type=="Local",]
local.cancellation.rate <- sum(local$Weight*local$Exit_precrisis)/(sum(local$Weight))
domestic <- pre_crisiscases[pre_crisiscases$Investor_Type=="Domestic",]
domestic.cancellation.rate <- sum(domestic$Weight*domestic$Exit_precrisis)/(sum(domestic$Weight))
intl <- pre_crisiscases[pre_crisiscases$Investor_Type=="International",]
intl.cancellation.rate <- sum(intl$Weight*intl$Exit_precrisis)/(sum(intl$Weight))

	#exits post-crisis#
postcrisis <- corrs[corrs$Exit_Year>2002,]
local <- postcrisis[postcrisis$Investor_Type=="Local",]
local.cancellation.rate <- sum(local$Weight*local$Exit)/(sum(local$Weight))
domestic <- postcrisis[postcrisis$Investor_Type=="Domestic",]
domestic.cancellation.rate <- sum(domestic$Weight*domestic$Exit)/(sum(domestic$Weight))
intl <- postcrisis[postcrisis$Investor_Type=="International",]
intl.cancellation.rate <- sum(intl$Weight*intl$Exit)/(sum(intl$Weight))


###ANOVAS OF INVESTOR TYPE AND CONTRACTUAL OUTCOMES### 

##for Investor Type and Exit Patterns#
lm.out <- lm(Exit ~ Investor_Type, weights=Weight, data=corrs) #For all three categories of investors#
summary(lm.out)
anova.lm(lm.out)
	
lm.out <- lm(Exit ~ Investor_Type2, weights=Weight, data=corrs) #Local investors versus domestic and international#
summary(lm.out)
anova.lm(lm.out)
	
	
#Anova for Investor Type and Post-Crisis Contract Renegotiation#
lm.out <- lm(PostCrisis_RenegFinal ~ Investor_Type, weights=Weight, data=corrs)
summary(lm.out)
anova.lm(lm.out)

	##Analysis with renegotiation coded as dichotomous variable where one reflects conclusion of full accord##
MNC <- corrs[corrs$Investor_Type=="International",] 
Dom <- corrs[corrs$Investor_Type=="Domestic",] 
Loc <- corrs[corrs$Investor_Type=="Local",] 

a <- sum(na.omit(MNC$PostCrisis_Ren_Dichot*MNC$Weight))
b <- sum(na.omit(as.numeric(MNC$PostCrisis_Ren_Dichot==0)*MNC$Weight))
c <- sum(na.omit(Dom$PostCrisis_Ren_Dichot*Dom$Weight))
d <- sum(na.omit(as.numeric(Dom$PostCrisis_Ren_Dichot==0)*Dom$Weight))
e <- sum(na.omit(Loc$PostCrisis_Ren_Dichot*Loc$Weight))
f <- sum(na.omit(as.numeric(Loc$PostCrisis_Ren_Dichot==0)*Loc$Weight))

data <- matrix(c(a, b, c, d, e, f), ncol=2, byrow=T)
chisq.test(data)	
fisher.test(data)

#Anova for Investor Type and Post-Crisis Tariff Increases#
lm.out <- lm(PostCrisis_Tariff_Percent ~ Investor_Type, weights=Weight, data=corrs)
summary(lm.out)
anova.lm(lm.out)



###ALIGNMENT Tables in On-line Appendix###

##Table A.II.1: General Correlations##
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Aligned2004_2007, Reneg2004_2007)) 
detach(corrs)
aligned <- Reneg.subset[Reneg.subset$Aligned2004_2007==1,]
print(aligned.reneg.rate <- sum(aligned$Weight*aligned$Reneg2004_2007)/(sum(aligned$Weight)))
unaligned <- Reneg.subset[Reneg.subset$Aligned2004_2007==0,]
print(unaligned.reneg.rate <- sum(unaligned$Weight*unaligned$Reneg2004_2007)/(sum(unaligned$Weight)))

attach(corrs)
Exit.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Aligned2004_2007, Exit2004_2007)) 
detach(corrs)
aligned <- Exit.subset[Exit.subset$Aligned2004_2007=="1",]
print(aligned.exit.rate <- sum(aligned$Weight*aligned$Exit2004_2007)/(sum(aligned$Weight)))
unaligned <- Exit.subset[Exit.subset$Aligned2004_2007==0,]
print(unaligned.exit.rate <- sum(unaligned$Weight*unaligned$Exit2004_2007)/(sum(unaligned$Weight)))

attach(corrs)
Tariff.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Aligned2004_2007, PostCrisisTariff2004_2007))
aligned <- Tariff.subset[Tariff.subset$Aligned2004_2007==1,]
print(aligned.tariff <- sum(aligned$Weight*aligned$PostCrisisTariff2004_2007)/(sum(aligned$Weight)))
unaligned <- Tariff.subset[Tariff.subset$Aligned2004_2007==0,]
print(unaligned.tariff<- sum(unaligned$Weight*unaligned$PostCrisisTariff2004_2007)/(sum(unaligned$Weight)))

##Statistical significance between rates in chart##
	#Difference in Exit Patterns#
lm.out <- lm(Exit2004_2007 ~ Aligned2004_2007, weights=Weight, data=Exit.subset)
summary(lm.out)
anova.lm(lm.out)

Aligned <- corrs[corrs$Aligned2004_2007==1,] 
Unaligned <- corrs[corrs$Aligned2004_2007==0,] 
a <- sum(na.omit(Aligned$Exit2004_2007*Aligned$Weight))
b <- sum(na.omit(as.numeric(Aligned$Exit2004_2007==0)*Aligned$Weight))
c <- sum(na.omit(Unaligned$Exit2004_2007*Unaligned$Weight))
d <- sum(na.omit(as.numeric(Unaligned$Exit2004_2007==0)*Unaligned$Weight))

data <- matrix(c(a, b, c, d), ncol=2, byrow=T)
chisq.test(data)	
fisher.test(data)

	#Difference in Renegotiation Rates#
lm.out <- lm(Reneg2004_2007 ~ Aligned2004_2007, weights=Weight, data=Reneg.subset)
summary(lm.out)
anova.lm(lm.out)
	#Difference in Tariff Increases#
lm.out <- lm(PostCrisisTariff2004_2007 ~ Aligned2004_2007, weights=Weight, data=Tariff.subset)
summary(lm.out)
anova.lm(lm.out)

##Table A.II.2.  Exit Rates Conditional on Investor Type and Alignment##
attach(corrs)
Exit.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Aligned2004_2007, Exit2004_2007)) 
detach(corrs)
local <- Exit.subset[Exit.subset$Investor_Type=="Local",]
local_aligned <- local[local$Aligned2004_2007==1,]
print(local.aligned.exit.rate <- sum(local_aligned$Weight*local_aligned$Exit2004_2007)/(sum(local_aligned$Weight)))
local_unaligned <- local[local$Aligned2004_2007==0,]
print(local.unaligned.exit.rate <- sum(local_unaligned$Weight*local_unaligned$Exit2004_2007)/(sum(local_unaligned$Weight)))

domestic <- Exit.subset[Exit.subset$Investor_Type=="Domestic",]
domestic.aligned <- domestic[domestic$Aligned2004_2007==1,]
print(domestic.aligned.exit.rate <- sum(domestic.aligned$Weight*domestic.aligned$Exit2004_2007)/(sum(domestic.aligned$Weight)))
domestic.unaligned <- domestic[domestic$Aligned2004_2007==0,]
print(domestic.unaligned.exit.rate <- sum(domestic.unaligned$Weight*domestic.unaligned$Exit2004_2007)/(sum(domestic.unaligned$Weight)))

intl <- Exit.subset[Exit.subset$Investor_Type=="International",]
intl.aligned <- intl[intl$Aligned2004_2007==1,]
print(intl.aligned.exit.rate <- sum(intl.aligned$Weight*intl.aligned$Exit2004_2007)/(sum(intl.aligned$Weight)))
intl.unaligned <- intl[intl$Aligned2004_2007==0,]
print(intl.unaligned.exit.rate <- sum(intl.unaligned$Weight*intl.unaligned$Exit2004_2007)/(sum(intl.unaligned$Weight)))

##Table A.II.3.  Renegotiation Rates Conditional on Investor Type and Alignment##
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Aligned2004_2007, Reneg2004_2007)) 
detach(corrs)
local <- Reneg.subset[Reneg.subset$Investor_Type=="Local",]
local_aligned <- local[local$Aligned2004_2007==1,]
print(local.aligned.reneg.rate <- sum(local_aligned$Weight*local_aligned$Reneg2004_2007)/(sum(local_aligned$Weight)))
local_unaligned <- local[local$Aligned2004_2007==0,]
print(local.unaligned.reneg.rate <- sum(local_unaligned$Weight*local_unaligned$Reneg2004_2007)/(sum(local_unaligned$Weight)))

domestic <- Reneg.subset[Reneg.subset$Investor_Type=="Domestic",]
domestic.aligned <- domestic[domestic$Aligned2004_2007==1,]
print(domestic.aligned.reneg.rate <- sum(domestic.aligned$Weight*domestic.aligned$Reneg2004_2007)/(sum(domestic.aligned$Weight)))
domestic.unaligned <- domestic[domestic$Aligned2004_2007==0,]
print(domestic.unaligned.reneg.rate <- sum(domestic.unaligned$Weight*domestic.unaligned$Reneg2004_2007)/(sum(domestic.unaligned$Weight)))

intl <- Reneg.subset[Reneg.subset$Investor_Type=="International",]
intl.aligned <- intl[intl$Aligned2004_2007==1,]
print(intl.aligned.reneg.rate <- sum(intl.aligned$Weight*intl.aligned$Reneg2004_2007)/(sum(intl.aligned$Weight)))
intl.unaligned <- intl[intl$Aligned2004_2007==0,]
print(intl.unaligned.reneg.rate <- sum(intl.unaligned$Weight*intl.unaligned$Reneg2004_2007)/(sum(intl.unaligned$Weight)))


##DISPERSION OF AUTHORITY Tables in On-line Appendix## 

##Table A.II.4#

	##Renegotiation Rates##
Reneg.data <- na.omit(data.frame(Investor_regime, Weight, Province, PostCrisis_RenegFinal, Dispersion_Categories)) 
Low.Cases <- Reneg.data[Reneg.data$Dispersion_Categories=="Low",]
sum(Low.Cases$Weight*Low.Cases$PostCrisis_RenegFinal)/sum(Low.Cases$Weight)
Medium.Cases <- Reneg.data[Reneg.data$Dispersion_Categories=="Medium",]
sum(Medium.Cases$PostCrisis_RenegFinal*Medium.Cases$Weight)/sum(Medium.Cases$Weight)
High.Cases <- Reneg.data[Reneg.data$Dispersion_Categories=="High",]
sum(High.Cases$PostCrisis_RenegFinal*High.Cases$Weight)/sum(High.Cases$Weight)

	##Exit Rates##
attach(corrs)
Exit.data <- na.omit(data.frame(Investor_regime, Weight, Province, Exit, Dispersion_Categories)) 
Low.Cases <- Exit.data[Exit.data$Dispersion_Categories=="Low",]
sum(Low.Cases$Weight*Low.Cases$Exit)/sum(Low.Cases$Weight)
Medium.Cases <- Exit.data[Exit.data$Dispersion_Categories=="Medium",]
sum(Medium.Cases$Exit*Medium.Cases$Weight)/sum(Medium.Cases$Weight)
High.Cases <- Exit.data[Exit.data$Dispersion_Categories=="High",]
sum(High.Cases$Exit*High.Cases$Weight)/sum(High.Cases$Weight)

	##Tariff Increases##
Tariff.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, PostCrisis_Tariff_Percent, Dispersion_Categories))
detach(corrs)
Low.Cases <- Tariff.subset[Tariff.subset$Dispersion_Categories=="Low",]
sum(Low.Cases$Weight*Low.Cases$PostCrisis_Tariff_Percent)/sum(Low.Cases$Weight)
Medium.Cases <- Tariff.subset[Tariff.subset$Dispersion_Categories=="Medium",]
sum(Medium.Cases$PostCrisis_Tariff_Percent*Medium.Cases$Weight)/sum(Medium.Cases$Weight)
High.Cases <- Tariff.subset[Tariff.subset$Dispersion_Categories=="High",]
sum(High.Cases$PostCrisis_Tariff_Percent*High.Cases$Weight)/sum(High.Cases$Weight)


##Conditional tables##
##Table A.II.5  Exit conditional on investor type and dispersion of authority##
attach(corrs)
Exit.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Dispersion_Categories, Exit)) 
detach(corrs)
local <- Exit.subset[Exit.subset$Investor_Type=="Local",]
local_low <- local[local$Dispersion_Categories=="Low",]
print(local.low.exit.rate <- sum(local_low$Weight*local_low$Exit)/(sum(local_low$Weight)))
local_med <- local[local$Dispersion_Categories=="Medium",]
print(local.med.exit.rate <- sum(local_med$Weight*local_med$Exit)/(sum(local_med$Weight)))
local_high <- local[local$Dispersion_Categories=="High",]
print(local.high.exit.rate <- sum(local_high$Weight*local_high$Exit)/(sum(local_high$Weight)))


domestic <- Exit.subset[Exit.subset$Investor_Type=="Domestic",]
domestic_low <- domestic[domestic$Dispersion_Categories=="Low",]
print(domestic.low.exit.rate <- sum(domestic_low$Weight*domestic_low$Exit)/(sum(domestic_low$Weight)))
domestic_med <- domestic[domestic$Dispersion_Categories=="Medium",]
print(domestic.med.exit.rate <- sum(domestic_med$Weight*domestic_med$Exit)/(sum(domestic_med$Weight)))
domestic_high <- domestic[domestic$Dispersion_Categories=="High",]
print(domestic.high.exit.rate <- sum(domestic_high$Weight*domestic_high$Exit)/(sum(domestic_high$Weight)))


intl <- Exit.subset[Exit.subset$Investor_Type=="International",]
intl_low <- intl[intl$Dispersion_Categories=="Low",]
print(intl.low.exit.rate <- sum(intl_low$Weight*intl_low$Exit)/(sum(intl_low$Weight)))
intl_med <- intl[intl$Dispersion_Categories=="Medium",]
print(intl.med.exit.rate <- sum(intl_med$Weight*intl_med$Exit)/(sum(intl_med$Weight)))
intl_high <- intl[intl$Dispersion_Categories=="High",]
print(intl.high.exit.rate <- sum(intl_high$Weight*intl_high$Exit)/(sum(intl_high$Weight)))


##Table A.II.5.  Renegotiation Post-Crisis conditional on investor type and dispersion of authority##
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Dispersion_Categories, PostCrisis_RenegFinal)) 
detach(corrs)
local <- Reneg.subset[Reneg.subset$Investor_Type=="Local",]
local_low <- local[local$Dispersion_Categories=="Low",]
print(local.low.reneg.rate <- sum(local_low$Weight*local_low$PostCrisis_RenegFinal)/(sum(local_low$Weight)))
local_med <- local[local$Dispersion_Categories=="Medium",]
print(local.med.reneg.rate <- sum(local_med$Weight*local_med$PostCrisis_RenegFinal)/(sum(local_med$Weight)))
local_high <- local[local$Dispersion_Categories=="High",]
print(local.high.reneg.rate <- sum(local_high$Weight*local_high$PostCrisis_RenegFinal)/(sum(local_high$Weight)))

domestic <- Reneg.subset[Reneg.subset$Investor_Type=="Domestic",]
domestic_low <- domestic[domestic$Dispersion_Categories=="Low",]
print(domestic.low.reneg.rate <- sum(domestic_low$Weight*domestic_low$PostCrisis_RenegFinal)/(sum(domestic_low$Weight)))
domestic_med <- domestic[domestic$Dispersion_Categories=="Medium",]
print(domestic.med.reneg.rate <- sum(domestic_med$Weight*domestic_med$PostCrisis_RenegFinal)/(sum(domestic_med$Weight)))
domestic_high <- domestic[domestic$Dispersion_Categories=="High",]
print(domestic.high.reneg.rate <- sum(domestic_high$Weight*domestic_high$PostCrisis_RenegFinal)/(sum(domestic_high$Weight)))

intl <- Reneg.subset[Reneg.subset$Investor_Type=="International",]
intl_low <- intl[intl$Dispersion_Categories=="Low",]
print(intl.low.reneg.rate <- sum(intl_low$Weight*intl_low$PostCrisis_RenegFinal)/(sum(intl_low$Weight)))
intl_med <- intl[intl$Dispersion_Categories=="Medium",]
print(intl.med.reneg.rate <- sum(intl_med$Weight*intl_med$PostCrisis_RenegFinal)/(sum(intl_med$Weight)))
intl_high <- intl[intl$Dispersion_Categories=="High",]
print(intl.high.reneg.rate <- sum(intl_high$Weight*intl_high$PostCrisis_RenegFinal)/(sum(intl_high$Weight)))


##Correlation between Dispersion of Authority and Contractual Outcomes##(None of correlations significant)
	##The raw dispersion of authority data, unfortunately, cannot be shared##
corrs <- read.csv("replicationdata2.csv", header=TRUE)  
cor.test(corrs$Exit, corrs$Dispersion_Authority, method="spearman", use="complete.obs")
cor.test(corrs$Exit, corrs$Dispersion_Authority, use="complete.obs")
cor.test(corrs$Exit, corrs$Dispersion_Authority, method="kendall", use="complete.obs")

cor.test(corrs$PostCrisis_RenegFinal, corrs$Dispersion_Authority, method="spearman", use="complete.obs")
cor.test(corrs$PostCrisis_RenegFinal, corrs$Dispersion_Authority, use="complete.obs")
cor.test(corrs$PostCrisis_RenegFinal, corrs$Dispersion_Authority, method="kendall", use="complete.obs")

cor.test(corrs$PostCrisis_Tariff_Percent, corrs$Dispersion_Authority, method="spearman", use="complete.obs")
cor.test(corrs$PostCrisis_Tariff_Percent, corrs$Dispersion_Authority, use="complete.obs")
cor.test(corrs$PostCrisis_Tariff_Percent, corrs$Dispersion_Authority, method="kendall", use="complete.obs")


##LOCAL STATE CAPACITY Tables in On-line Appendix## 
 
detach(corrs)
corrs <- read.csv("replicationdata.csv", header=TRUE)

##Table A.II.8#

	##Renegotiation Rates##
Reneg.data <- na.omit(data.frame(Investor_regime, Weight, Province, PostCrisis_RenegFinal, State_Capacity_Score)) 
Low.Cases <- Reneg.data[Reneg.data$State_Capacity_Score==3,]
sum(Low.Cases$Weight*Low.Cases$PostCrisis_RenegFinal)/sum(Low.Cases$Weight)
Medium.Cases <- Reneg.data[Reneg.data$State_Capacity_Score==4,]
sum(Medium.Cases$PostCrisis_RenegFinal*Medium.Cases$Weight)/sum(Medium.Cases$Weight)
High.Cases <- Reneg.data[Reneg.data$State_Capacity_Score==5,]
sum(High.Cases$PostCrisis_RenegFinal*High.Cases$Weight)/sum(High.Cases$Weight)

	##Exit Rates##
attach(corrs)
Exit.data <- na.omit(data.frame(Investor_regime, Weight, Province, Exit, State_Capacity_Score)) 
Low.Cases <- Exit.data[Exit.data$State_Capacity_Score==3,]
sum(Low.Cases$Weight*Low.Cases$Exit)/sum(Low.Cases$Weight)
Medium.Cases <- Exit.data[Exit.data$State_Capacity_Score==4,]
sum(Medium.Cases$Exit*Medium.Cases$Weight)/sum(Medium.Cases$Weight)
High.Cases <- Exit.data[Exit.data$State_Capacity_Score==5,]
sum(High.Cases$Exit*High.Cases$Weight)/sum(High.Cases$Weight)


	##Tariff Increases##
Tariff.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, PostCrisis_Tariff_Percent, State_Capacity_Score))
detach(corrs)
Low.Cases <- Tariff.subset[Tariff.subset$State_Capacity_Score==3,]
sum(Low.Cases$Weight*Low.Cases$PostCrisis_Tariff_Percent)/sum(Low.Cases$Weight)
Medium.Cases <- Tariff.subset[Tariff.subset$State_Capacity_Score==4,]
sum(Medium.Cases$PostCrisis_Tariff_Percent*Medium.Cases$Weight)/sum(Medium.Cases$Weight)
High.Cases <- Tariff.subset[Tariff.subset$State_Capacity_Score==5,]
sum(High.Cases$PostCrisis_Tariff_Percent*High.Cases$Weight)/sum(High.Cases$Weight)


##STATE CAPACITY CORRELATIONS##

	##Exit correlations positive, insignificant##
cor.test(corrs$Exit, corrs$State_Capacity_Score, method="spearman", use="complete.obs")
cor.test(corrs$Exit, corrs$State_Capacity_Score, use="complete.obs")
cor.test(corrs$Exit, corrs$State_Capacity_Score, method="kendall", use="complete.obs")

lm.out <- lm(Exit ~ State_Capacity_Score, weights=Weight, data=corrs)
summary(lm.out)
anova.lm(lm.out)

Low <- corrs[corrs$State_Capacity_Score==3,] 
Med <- corrs[corrs$State_Capacity_Score==4,] 
High <- corrs[corrs$State_Capacity_Score==5,] 
a <- sum(na.omit(Low$Exit*Low$Weight))
b <- sum(na.omit(as.numeric(Low$Exit==0)*Low$Weight))
c <- sum(na.omit(Med$Exit*Med$Weight))
d <- sum(na.omit(as.numeric(Med$Exit==0)*Med$Weight))
e <- round(sum(na.omit(High$Exit*High$Weight)))
f <- sum(na.omit(as.numeric(High$Exit==0)*High$Weight))

data <- matrix(c(a, b, c, d, e, f), ncol=2, byrow=T)
chisq.test(data)	
fisher.test(data)

	#Renegotiation correlations positive and insignificant##
cor.test(corrs$PostCrisis_RenegFinal, corrs$State_Capacity_Score, method="spearman", use="complete.obs")
cor.test(corrs$PostCrisis_RenegFinal, corrs$State_Capacity_Score, use="complete.obs")
cor.test(corrs$PostCrisis_RenegFinal, corrs$State_Capacity_Score, method="kendall", use="complete.obs")

	##Tariff increases: Relationship is positive and Insignificant#
cor.test(corrs$PostCrisis_Tariff_Percent, corrs$State_Capacity_Score, method="spearman", use="complete.obs")
cor.test(corrs$PostCrisis_Tariff_Percent, corrs$State_Capacity_Score, use="complete.obs")
cor.test(corrs$PostCrisis_Tariff_Percent, corrs$State_Capacity_Score, method="kendall", use="complete.obs")

##Conditional tables##
##Table A.II.9  Exit conditional on investor type and local state capacity##
detach(corrs)
corrs <- read.csv("replicationdata.csv", header=TRUE)
attach(corrs)
Exit.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, State_Capacity_Score, Exit)) 
detach(corrs)
local <- Exit.subset[Exit.subset$Investor_Type=="Local",]
local_low <- local[local$State_Capacity_Score==3,]
print(local.low.exit.rate <- sum(local_low$Weight*local_low$Exit)/(sum(local_low$Weight)))
local_med <- local[local$State_Capacity_Score==4,]
print(local.med.exit.rate <- sum(local_med$Weight*local_med$Exit)/(sum(local_med$Weight)))
local_high <- local[local$State_Capacity_Score==5,]
print(local.high.exit.rate <- sum(local_high$Weight*local_high$Exit)/(sum(local_high$Weight)))

domestic <- Exit.subset[Exit.subset$Investor_Type=="Domestic",]
domestic_low <- domestic[domestic$State_Capacity_Score==3,]
print(domestic.low.exit.rate <- sum(domestic_low$Weight*domestic_low$Exit)/(sum(domestic_low$Weight)))
domestic_med <- domestic[domestic$State_Capacity_Score==4,]
print(domestic.med.exit.rate <- sum(domestic_med$Weight*domestic_med$Exit)/(sum(domestic_med$Weight)))
domestic_high <- domestic[domestic$State_Capacity_Score==5,]
print(domestic.high.exit.rate <- sum(domestic_high$Weight*domestic_high$Exit)/(sum(domestic_high$Weight)))

intl <- Exit.subset[Exit.subset$Investor_Type=="International",]
intl_low <- intl[intl$State_Capacity_Score==3,]
print(intl.low.exit.rate <- sum(intl_low$Weight*intl_low$Exit)/(sum(intl_low$Weight)))
intl_med <- intl[intl$State_Capacity_Score==4,]
print(intl.med.exit.rate <- sum(intl_med$Weight*intl_med$Exit)/(sum(intl_med$Weight)))
intl_high <- intl[intl$State_Capacity_Score==5,]
print(intl.high.exit.rate <- sum(intl_high$Weight*intl_high$Exit)/(sum(intl_high$Weight)))


##Table A.II.10  Renegotiation Post-Crisis conditional on investor type and local state capacity##
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, State_Capacity_Score, PostCrisis_RenegFinal)) 
detach(corrs)
local <- Reneg.subset[Reneg.subset$Investor_Type=="Local",]
local_low <- local[local$State_Capacity_Score==3,]
print(local.low.reneg.rate <- sum(local_low$Weight*local_low$PostCrisis_RenegFinal)/(sum(local_low$Weight)))
local_med <- local[local$State_Capacity_Score==4,]
print(local.med.reneg.rate <- sum(local_med$Weight*local_med$PostCrisis_RenegFinal)/(sum(local_med$Weight)))
local_high <- local[local$State_Capacity_Score==5,]
print(local.high.reneg.rate <- sum(local_high$Weight*local_high$PostCrisis_RenegFinal)/(sum(local_high$Weight)))

domestic <- Reneg.subset[Reneg.subset$Investor_Type=="Domestic",]
domestic_low <- domestic[domestic$State_Capacity_Score==3,]
print(domestic.low.reneg.rate <- sum(domestic_low$Weight*domestic_low$PostCrisis_RenegFinal)/(sum(domestic_low$Weight)))
domestic_med <- domestic[domestic$State_Capacity_Score==4,]
print(domestic.med.reneg.rate <- sum(domestic_med$Weight*domestic_med$PostCrisis_RenegFinal)/(sum(domestic_med$Weight)))
domestic_high <- domestic[domestic$State_Capacity_Score==5,]
print(domestic.high.reneg.rate <- sum(domestic_high$Weight*domestic_high$PostCrisis_RenegFinal)/(sum(domestic_high$Weight)))


intl <- Reneg.subset[Reneg.subset$Investor_Type=="International",]
intl_low <- intl[intl$State_Capacity_Score==3,]
print(intl.low.reneg.rate <- sum(intl_low$Weight*intl_low$PostCrisis_RenegFinal)/(sum(intl_low$Weight)))
intl_med <- intl[intl$State_Capacity_Score==4,]
print(intl.med.reneg.rate <- sum(intl_med$Weight*intl_med$PostCrisis_RenegFinal)/(sum(intl_med$Weight)))
intl_high <- intl[intl$State_Capacity_Score==5,]
print(intl.high.reneg.rate <- sum(intl_high$Weight*intl_high$PostCrisis_RenegFinal)/(sum(intl_high$Weight)))


##REGULATORY AGENCY DESIGN ANALYSIS##

detach(corrs)
corrs <- read.csv("replicationdata.csv", header=TRUE)

##Table A.II.12: Regulatory Independence and Contractual Outcomes#

	##Renegotiation Rates##
attach(corrs)
Reneg.data <- na.omit(data.frame(Investor_regime, Weight, Province, PostCrisis_RenegFinal, Allow_Appeals)) 
Low.Cases <- Reneg.data[Reneg.data$Allow_Appeals==0,]
sum(Low.Cases$Weight*Low.Cases$PostCrisis_RenegFinal)/sum(Low.Cases$Weight)
High.Cases <- Reneg.data[Reneg.data$Allow_Appeals==1,]
sum(High.Cases$PostCrisis_RenegFinal*High.Cases$Weight)/sum(High.Cases$Weight)

	##Exit Rates##
Exit.data <- na.omit(data.frame(Investor_regime, Weight, Province, Exit, Allow_Appeals)) 
Low.Cases <- Exit.data[Exit.data$Allow_Appeals==0,]
sum(Low.Cases$Weight*Low.Cases$Exit)/sum(Low.Cases$Weight)
High.Cases <- Exit.data[Exit.data$Allow_Appeals==1,]
sum(High.Cases$Exit*High.Cases$Weight)/sum(High.Cases$Weight)

	##Tariff Increases##
Tariff.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, PostCrisis_Tariff_Percent, Allow_Appeals))
detach(corrs)
Low.Cases <- Tariff.subset[Tariff.subset$Allow_Appeals==0,]
sum(Low.Cases$Weight*Low.Cases$PostCrisis_Tariff_Percent)/sum(Low.Cases$Weight)
High.Cases <- Tariff.subset[Tariff.subset$Allow_Appeals==1,]
sum(High.Cases$PostCrisis_Tariff_Percent*High.Cases$Weight)/sum(High.Cases$Weight)


	##Fisher exact test with base data#
Allow_Appeals <- corrs[corrs$Allow_Appeals==1,] 
No_Appeals <- corrs[corrs$Allow_Appeals==0,] 
a <- sum(na.omit(Allow_Appeals$Exit*Allow_Appeals$Weight))
b <- sum(na.omit(as.numeric(Allow_Appeals$Exit==0)*Allow_Appeals$Weight))
c <- round(sum(na.omit(No_Appeals$Exit*No_Appeals$Weight)))
d <- sum(na.omit(as.numeric(No_Appeals$Exit==0)*No_Appeals$Weight))
data <- matrix(c(a, b, c, d), ncol=2, byrow=T)
chisq.test(data)	
fisher.test(data)


##Conditional tables##
##Table A.II.13  Exit conditional on investor type and regulatory independence##
detach(corrs)
corrs <- read.csv("replicationdata.csv", header=TRUE)
attach(corrs)
Exit.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Allow_Appeals, Exit)) 
detach(corrs)
local <- Exit.subset[Exit.subset$Investor_Type=="Local",]
local_noappeal <- local[local$Allow_Appeals==0,]
print(local.noappeal.exit.rate <- sum(local_noappeal$Weight*local_noappeal$Exit)/(sum(local_noappeal$Weight)))
local_appeal <- local[local$Allow_Appeals==1,]
print(local.appeal.exit.rate <- sum(local_appeal$Weight*local_appeal$Exit)/(sum(local_appeal$Weight)))

domestic <- Exit.subset[Exit.subset$Investor_Type=="Domestic",]
domestic_noappeal <- domestic[domestic$Allow_Appeals==0,]
print(domestic.noappeal.exit.rate <- sum(domestic_noappeal$Weight*domestic_noappeal$Exit)/(sum(domestic_noappeal$Weight)))
domestic_appeal <- domestic[domestic$Allow_Appeals==1,]
print(domestic.appeal.exit.rate <- sum(domestic_appeal$Weight*domestic_appeal$Exit)/(sum(domestic_appeal$Weight)))

intl <- Exit.subset[Exit.subset$Investor_Type=="International",]
intl_noappeal <- intl[intl$Allow_Appeals==0,]
print(intl.noappeal.exit.rate <- sum(intl_noappeal$Weight*intl_noappeal$Exit)/(sum(intl_noappeal$Weight)))
intl_appeal <- intl[intl$Allow_Appeals==1,]
print(intl.appeal.exit.rate <- sum(intl_appeal$Weight*intl_appeal$Exit)/(sum(intl_appeal$Weight)))


##Table A.II.14.  Renegotiation Post-Crisis conditional on investor type and Regulatory Independence##
attach(corrs)
Reneg.subset <- na.omit(data.frame(Investor_regime, Investor_Type, Weight, Allow_Appeals, PostCrisis_RenegFinal)) 
detach(corrs)
local <- Reneg.subset[Reneg.subset$Investor_Type=="Local",]
local_low <- local[local$Allow_Appeals==0,]
print(local.low.reneg.rate <- sum(local_low$Weight*local_low$PostCrisis_RenegFinal)/(sum(local_low$Weight)))
local_high <- local[local$Allow_Appeals==1,]
print(local.high.reneg.rate <- sum(local_high$Weight*local_high$PostCrisis_RenegFinal)/(sum(local_high$Weight)))

domestic <- Reneg.subset[Reneg.subset$Investor_Type=="Domestic",]
domestic_low <- domestic[domestic$Allow_Appeals==0,]
print(domestic.low.reneg.rate <- sum(domestic_low$Weight*domestic_low$PostCrisis_RenegFinal)/(sum(domestic_low$Weight)))
domestic_high <- domestic[domestic$Allow_Appeals==1,]
print(domestic.high.reneg.rate <- sum(domestic_high$Weight*domestic_high$PostCrisis_RenegFinal)/(sum(domestic_high$Weight)))

intl <- Reneg.subset[Reneg.subset$Investor_Type=="International",]
intl_low <- intl[intl$Allow_Appeals==0,]
print(intl.low.reneg.rate <- sum(intl_low$Weight*intl_low$PostCrisis_RenegFinal)/(sum(intl_low$Weight)))
intl_high <- intl[intl$Allow_Appeals==1,]
print(intl.high.reneg.rate <- sum(intl_high$Weight*intl_high$PostCrisis_RenegFinal)/(sum(intl_high$Weight)))






